<?php

namespace app\admin\controller\wanlshop;

use app\admin\model\User;
use app\common\controller\Backend;
use Exception;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Lifeend extends Backend
{

    /**
     * Lifeend模型对象
     * @var \app\admin\model\wanlshop\Lifeend
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\wanlshop\Lifeend;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("paymentStatusL", $this->model->getPaymentStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


     public function info()
     {
        $params = $this->request->param();
        $id = $params['id'];
        if (!$id) return $this->error('请稍候重试');
        $row = $this->model->find($id);
        if (!$row) return $this->error('未找到记录');
        return $this->view->fetch('info',['row' => $row]);

     }


     public function payment()
     {
        if (!$this->request->isAjax() || !$this->request->isPost()) return $this->error('请稍候重试');
        $params = $this->request->param();
        $row = $this->model->find($params['id']);
        $row->payment_status = '4';
        $result = $row->save();
        if ($result === false) return $this->error('请稍候重试');
        return $this->success('操作成功');

     }


     public function examine ()
     {
        $params = $this->request->param();
        $row = $this->model->with(['user'])->find($params['id']);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
            $this->error(__('You have no permission'));
        }
        if ($this->request->isAjax()) {
            if (!in_array($params['status'], ['successed','rejected'])) return $this->error('请稍候重试');
            $this->model->startTrans();
            try {
                $row->status = $params['status'];
                $result = $row->save();
                

                if ($result !== false && $params['status'] == 'successed') {
                    // TODO 注销账号

                    $row->user->status = 'hidden';
                    $result = $row->user->save();
                }
                if ($result === false) throw new Exception();
                $this->model->commit();
            } catch (Exception $e) {
                $this->model->rollback();
                return $this->error('请稍候重试');
            }
            return $this->success('操作成功');
        } else {
            if (false === $this->request->isPost()) {
                $this->view->assign('row', $row);
                return $this->view->fetch();
            }
        }
     }

}
